import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Class describe Language entity allow to access to DB 
 * Created language never update
 * @author anton
 * @version 0.01
 */
public class EW_Languages extends DataBaseAccess {
	
	/**
	 * Class constructor 
	 */
	public EW_Languages() {
		super("Languages");
	}

	private String name = ""; // language name
	private boolean isExist = false; // variable for checking is need to insert to db

	@Override
	protected final ResultSet getListByDetail(HashMap<?, ?> keys) {
		String selectSQL = "SELECT * From Languages";
		try {
			EWDataBase db = EWDataBase.getInstance();
			return db.conn.prepareStatement(selectSQL).executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get all languages in DB
	 * @return array list of languages
	 */
	public ArrayList<EW_Languages> getListInDB(){
		ResultSet rs = getListByDetail(null);
		ArrayList<EW_Languages> myData = new ArrayList<EW_Languages>();
		try {
			while (rs.next()) {
				EW_Languages item = new EW_Languages();
				item.isExist = true;
				item.setName(rs.getString(1));
				myData.add(item);
			}
			rs.close();
			return myData;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * try to find language in DB by language name
	 * @param languageName - language name for search in DB
	 * @return
	 * 		return null if exception occur
	 * 		return new instance of class if language is not find <br> 
	 * 		return object of EW_Languages from DB if name is equal to languageName
	 */
	public static EW_Languages GetOneRowByName(String languageName){
		EW_Languages newInstance = new EW_Languages();
		String selectSQL = "SELECT * From Languages WHERE name = '" + languageName + "'";
		try {
			EWDataBase db = EWDataBase.getInstance();
			ResultSet rs = db.conn.prepareStatement(selectSQL).executeQuery();
			if (rs.next()) {
				newInstance.isExist = true;
				newInstance.setName(rs.getString(1));
			} else {
				newInstance.isExist = false;
				newInstance.setName(languageName);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return newInstance;
	}
	
	/**
	 * delete current record from DB
	 */
	public int deleteOneRecord(String languageName){
		if (!isExist)
			return DataBaseAccess.TABLE_DOES_NOT_EXISTS;
		ArrayList<String> keys = new ArrayList<String>();
		keys.add("name = '"+languageName+"'");
		return super.deleteOneRecord(keys);
	}
	/**
	 * insert current record to DB
	 * @return
	 * 		integer result of insert or DataBaseAccess.RECORD_EXISTS_IN_TABLE if language geted from DB
	 */
	public int insertCurRecord(){
		if (isExist) 
			return DataBaseAccess.RECORD_EXISTS_IN_TABLE;
		HashMap<String, String> insertValues = new HashMap<String, String>();
		insertValues.put("NAME", name);
		return super.insertOneRecord(insertValues);
	}
	
	/**
	 * get current language name
	 * @return
	 * 		String - current language name
	 */
	public String getName(){
		return name;
	}
	
	/**
	 * Set new language name
	 * @param newName - String new language name
	 */
	public void setName(String newName){
		name = newName;
	}
	
	/**
	 * is exist record in data base.
	 * @return	true if exist
	 */
	public boolean getIsExist(){
		return isExist;
	}

}
